import type { Role } from '../role/model';
import type { SysUser, UserPasswordForm } from './model';

import type { ID, PageResult } from '#/api/common';

import { requestClient } from '#/api/request';

enum Api {
  userAssignRole = '/sysUser/v1/updateUserRole',
  userDelete = '/sysUser/v1/deleteSysUser',
  userList = '/sysUser/v1/pageList',
  userRoleQuery = '/sysUser/v1/showUserRole',
  userSave = '/sysUser/v1/save',
  userUpdate = '/sysUser/v1/update',
  userUpdatePassword = '/sysUser/v1/updatePassword',
}

/**
 * 用户分页列表
 * @param data 请求参数
 * @returns 列表
 */
export function userList(data?: any) {
  return requestClient.post<PageResult<SysUser>>(Api.userList, data);
}

/**
 * 新增用户
 * @param data 参数
 */
export function userAdd(data: Partial<SysUser>) {
  return requestClient.postWithMsg<void>(Api.userSave, data);
}

/**
 * 更新用户
 * @param data 参数
 */
export function userUpdate(data: Partial<SysUser>) {
  return requestClient.postWithMsg<void>(Api.userUpdate, data);
}

/**
 * 删除用户
 * @param userId 用户ID
 */
export function userRemove(userId: ID) {
  return requestClient.get<void>(`${Api.userDelete}?userId=${userId}`);
}

/**
 * 修改用户密码
 * @param data 密码数据
 */
export function userUpdatePassword(data: UserPasswordForm) {
  return requestClient.postWithMsg<void>(Api.userUpdatePassword, data);
}

/**
 * 用户角色查询
 * @param userId 用户ID
 */
export function userRoleQuery(userId: ID) {
  return requestClient.get<Role[]>(`${Api.userRoleQuery}?userId=${userId}`);
}

/**
 * 角色分配
 * @param data 参数
 */
export function userAssignRole(data: {
  roleIdList: string[];
  userId: number | string;
}) {
  return requestClient.postWithMsg<void>(`${Api.userAssignRole}`, data);
}
